/* eslint-disable max-len */
import {request, handleFetchResultNotification, REQUEST_TYPE, formatPercent} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, Brands, List, Dealer, DealerSimple, Detail, Claim, ClaimDetail, OutClaim, OutClaimDetail} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {claimStatus, outClaimApplyStatus, baseDataStatus} from '../Enum';
import {PAGE} from '../constants';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '扣补款单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交{resourceName}'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});


const dealerDesc = formatMessage({
    id: 'api.message.dealerDesc',
    defaultMessage: '查询服务站'
});


const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});


const passDesc = formatMessage({
    id: 'api.message.passDesc',
    defaultMessage: '审核通过'
});

const rejectDesc = formatMessage({
    id: 'api.message.rejectDesc',
    defaultMessage: '审核不通过'
});

const passPiDesc = formatMessage({
    id: 'api.message.passPiDesc',
    defaultMessage: '确认通过'
});

const rejectPiDesc = formatMessage({
    id: 'api.message.rejectPiDesc',
    defaultMessage: '确认驳回'
});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const claimDesc = formatMessage({
    id: 'api.message.claimDesc',
    defaultMessage: '查询维修索赔单'
});

const confirmInfoDesc = formatMessage({
    id: 'api.message.confirmInfoDesc',
    defaultMessage: '确认'
});

const reportFinanceInfoDesc = formatMessage({
    id: 'api.message.reportFinanceInfoDesc',
    defaultMessage: '上报财务'
});

const exportPiDesc = formatMessage({
    id: 'api.message.exportPiDesc',
    defaultMessage: '导出Pi'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/expenseAdjustment/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, initQuery));

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/expenseAdjustments/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 提交
export const submitInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/submit`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));

//上报财务
export const reportFinanceInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/reportFinance`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, reportFinanceInfoDesc));

// 导出
export const exportInfo = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/expenseAdjustments/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};

// 查询服务站
export const getDealers = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dealers/forServiceInformation?${queryString}`, null, Dealer)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, dealerDesc));
};

// 快速查询服务站
export const getDealerSimple = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/dealers/forServiceInformation/simple?${queryString}`, null, DealerSimple)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, dealerDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/expenseAdjustments/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));


// 审核通过
export const passInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/pass`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passDesc));

// 审核不通过
export const rejectInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/reject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDesc));

// 确认
export const confirmInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/confirm`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmInfoDesc));

// 新增
export const addInfo = data => request(`${baseUrl}/expenseAdjustments`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 查询维修索赔单

// 查询服务站
export const getClaim = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claims/forCurrentUser?${queryString}`, null, Claim)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, claimDesc));
};

// 查询维修索赔单
export const getClaimSimple = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claims/forCurrentUser?${queryString}`, null, Claim)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, claimDesc))
        .then(res => {
            if(res.ok) return res.data.content;
            return false;
        });
};

export const getClaimDetail = id => request(`${baseUrl}/claims/${id}`, null, ClaimDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, claimDesc));

//导出扣款单
export const exportDetail = id => request(`${baseUrl}/expenseAdjustments/${id}/export`, null).then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc));

//导出PI
export const exportPi = id => request(`${baseUrl}/expenseAdjustments/${id}/exportPi`, null).then(handleFetchResultNotification(REQUEST_TYPE.submit, exportPiDesc));

// 审核通过
export const passPiInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/confirmPiPass`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passPiDesc));

// 审核不通过
export const rejectPiInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/confirmPiReject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectPiDesc));

// 多级审核通过
export const morePassInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/approve/pass`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passDesc));

// 多级审核不通过
export const moreRejectInfo = (id, data) => request(`${baseUrl}/expenseAdjustments/${id}/approve/reject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDesc));
// TODO 缺少善意索赔单和技术升级单 
// export const getOutClaim = queryCondition => {
//     const queryString = stringify({
//         ...queryCondition,
//         status: [outClaimApplyStatus.审核通过]
//     });
//     return request(`${baseUrl}/outClaimApplies/forCurrentUser?${queryString}`, null, OutClaim)
//         .then(handleFetchResultNotification(REQUEST_TYPE.query, outClaimDesc));
// };


// export const getOutClaimSimple = queryCondition => {
//     const queryString = stringify({
//         ...queryCondition,
//         status: [outClaimApplyStatus.审核通过]
//     });
//     return request(`${baseUrl}/outClaimApplies/forCurrentUser?${queryString}`, null, OutClaim)
//         .then(handleFetchResultNotification(REQUEST_TYPE.query, outClaimDesc))
//         .then(res => {
//             if(res.ok) return res.data.content;
//             return false;
//         });
// };

// export const getOutClaimDetail = id => request(`${baseUrl}/outClaimApplies/${id}`, null, OutClaimDetail)
//     .then(handleFetchResultNotification(REQUEST_TYPE.query, outClaimDesc));

